import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/components/login.vue";
import Home from "@/components/home.vue";
import Welcome from "@/components/welcome.vue";
import Users from "@/components/users/users.vue";
import Rights from "@/components/power/rights.vue";
import Roles from "@/components/power/roles.vue";
import Goods from "@/components/goods/goods.vue";
import Params from "@/components/goods/params.vue";
import Categories from "@/components/goods/categories.vue";
import Orders from "@/components/orders/orders.vue";
import Reports from "@/components/reports/reports.vue";
import Add from "@/components/goods/add.vue"
Vue.use(VueRouter);

const routes = [
  // 重定向：访问/ 自动定向到/login
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome },
      { path: "/users", component: Users },
      { path: "/rights", component: Rights },
      { path: "/roles", component: Roles },
      { path: "/goods", component: Goods },
      { path: "/params", component: Params },
      { path: "/categories", component: Categories },
      { path: "/orders", component: Orders },
      { path: "/reports", component: Reports },
      { path: "/add" , component: Add },
    ]
  }
];

const router = new VueRouter({
  routes
});
//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  //to:将要访问的路径
  //from:代表从哪个路径跳转而来
  //next:表示放行
  // next()  放行  next('/login') 强制跳转到xxx界面: '/xxx'
  if (to.path == "/login") return next();
  const tokenStr = window.sessionStorage.getItem("token");
  if (!tokenStr) return next("/login"); //强制跳转到login界面
  next();
});
export default router;
